package com.itbour.web.action.material;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.itbour.com.page.Page;
import com.itbour.common.base.BaseAction;
import com.itbour.common.util.StringUtils;
import com.itbour.dto.com.MediaInfoDto;
import com.itbour.dto.com.UserInfoDto;
import com.itbour.service.material.MediaInfoService;
import com.opensymphony.xwork2.ActionContext;


/**	媒体信息Action
 * 	@author LQ
 */
public class GetMediaInfoAction extends BaseAction {

	/**
	 */
	private static final long serialVersionUID = 1L;
	private MediaInfoService mediaInfoService;
	
	/*	分页获取 音频和视频列表信息
	 * 	参数:page/rows
	 * 	参数：查找用户ID
	 * 	author:李琦
	 */
	private Map<String, Object> mapResult =new HashMap<String, Object>();
	private String findMediaType;
	private String findMediaName;
	
	public String getMediaInfo() throws Exception {
		// 用户信息
		Map<String, Object> session = ActionContext.getContext().getSession();
    	UserInfoDto user = (UserInfoDto)session.get("user");
    	Map<String, Object> param = new HashMap<String, Object>();
		if(!user.isAdmin()) {
			param.put("user_id", user.getUser_id());// 默认检索自己上传的字体
		} 
		if (StringUtils.isNotEmpty(findMediaType)){
			param.put("media_type",findMediaType);
		}
		if (StringUtils.isNotEmpty(findMediaName)){
			param.put("media_name",findMediaName);
		}
		HttpServletRequest request = ServletActionContext.getRequest();
		String pageNumber = request.getParameter("page");
		String pageSize = request.getParameter("rows");
		Page page = mediaInfoService.pageGetMediaInfo(param, Integer.parseInt(pageNumber),
				Integer.parseInt(pageSize));
		HashMap<String, Object> resMap = new HashMap<String, Object>();
		resMap.put("total", page.getTotalCount());
		resMap.put("rows", page.getResult());
		mapResult = resMap;
		return SUCCESS;
	}
	
	private MediaInfoDto meDto;
	public void addMedia() throws Exception {// 新建媒体信息
		Map<String, Object> session = ActionContext.getContext().getSession();
		UserInfoDto user = (UserInfoDto)session.get("user");
		if (user!=null && user.getUser_id()!=null && meDto!=null){
			try {
				meDto.setUser_id(user.getUser_id());
				meDto.setUser_type(user.getUser_type());// 用户类型
				mediaInfoService.addMedia(meDto);
				this.writeJSON("ok");
			} catch (Exception e) {
				this.writeJSON("no");
			}
		} else {
			this.writeJSON("no");
		}
	}
	
	
	
	
	
	public MediaInfoDto getMeDto() {
		return meDto;
	}
	public void setMeDto(MediaInfoDto meDto) {
		this.meDto = meDto;
	}
	public String getFindMediaType() {
		return findMediaType;
	}
	public void setFindMediaType(String findMediaType) {
		this.findMediaType = findMediaType;
	}
	public String getFindMediaName() {
		return findMediaName;
	}
	public void setFindMediaName(String findMediaName) {
		this.findMediaName = findMediaName;
	}
	public Map<String, Object> getMapResult() {
		return mapResult;
	}
	public void setMapResult(Map<String, Object> mapResult) {
		this.mapResult = mapResult;
	}
	public static long getSerialversionuid() {
		return serialVersionUID;
	}
	public MediaInfoService getMediaInfoService() {
		return mediaInfoService;
	}
	public void setMediaInfoService(MediaInfoService mediaInfoService) {
		this.mediaInfoService = mediaInfoService;
	}
}
